/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edu.kamai.media.audio;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javazoom.jl.player.Player;

/**
 *
 * @author xyzhu
 */
public class mp3Player {
    
    private String filename;
    private Player player; 

    // constructor that takes the name of an MP3 file
    public mp3Player(String filename) {
        this.filename = filename;
    }

    public boolean isFinished(){
        if(player != null){
            return player.isComplete();
        }
        return false;
    }
    
    public void close() { if (player != null) player.close(); }

    // play the MP3 file to the sound card
    public void play() {
        try {
            FileInputStream fis = new FileInputStream(filename);
            BufferedInputStream bis = new BufferedInputStream(fis);
            player = new Player(bis);
        }
        catch (Exception e) {
            System.out.println("Problem playing file " + filename);
            System.out.println(e);
        }

        // run in new thread to play in background
        new Thread() {
            public void run() {
                try { player.play(); }
                catch (Exception e) { System.out.println(e); }
            }
        }.start();
}

    
    public static void main(String args[]){
        String filename = "D:\\Putclub\\VOA_Standard\\MP3\\putclub.com_20110930VOA.mp3";
        filename = "C:\\Users\\user\\kamai\\37ec03feb6803c26b67b3cf9f9e8932d.mp3";
        filename = "C:\\Users\\user\\kamai\\87c854d67de5069021fc824bde909e02.wma";
        filename = "C:\\Users\\xzhu\\kamai\\d11adca8b6abe25905fab4503c065575.wma";
        mp3Player mp3 = new mp3Player(filename);
        mp3.play();

        // do whatever computation you like, while music plays
        int N = 4000;
        double sum = 0.0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                sum += Math.sin(i + j);
            }
        }
        System.out.println(sum);
        
        // when the computation is done, stop playing it
        mp3.close();

        // play from the beginning
        mp3 = new mp3Player(filename);
        mp3.play();

    }
}

